<template>
  <div>
    <van-nav-bar
      title="新增地址"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
    />
    <van-address-edit
      :area-list="areaList"
      show-postal
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @change-detail="onChangeDetail"
    />
  </div>
</template>
<script>
import { areaList } from "@vant/area-data";
export default {
  data() {
    return {
      areaList,
      searchResult: [],

      address: {
        name: "",
        tel: "",
        address: "",
        addressDetail: "",
        isDefault: "",
        userID: 0,
        time: "",
        postalCode: "",
        areaCode: "",
      },
    };
  },
  created() {},
  methods: {
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: "黄龙万科中心",
            address: "杭州市西湖区",
          },
        ];
      } else {
        this.searchResult = [];
      }
    },
    onSave(content) {
      this.address.name = content.name;
      this.address.tel = content.tel;
      this.address.address = content.province + content.city + content.county;
      this.address.addressDetail = content.addressDetail;
      this.address.isDefault = content.isDefault;
      this.address.postalCode = content.postalCode;
      this.address.areaCode = content.areaCode;

      this.address.userID = this.$store.state.emailLogin[0].userID;
      var date = new Date();

      var year = date.getFullYear(); //年 ,从 Date 对象以四位数字返回年份
      var month = date.getMonth() + 1; //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
      var day = date.getDate(); //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)

      var hours = date.getHours(); //小时 ,返回 Date 对象的小时 (0 ~ 23)
      var minutes = date.getMinutes(); //分钟 ,返回 Date 对象的分钟 (0 ~ 59)
      var seconds = date.getSeconds(); //秒 ,返回 Date 对象的秒数 (0 ~ 59)
      var currentFormatDate =
        year +
        "-" +
        month +
        "-" +
        day +
        " " +
        hours +
        ":" +
        minutes +
        ":" +
        seconds;
      this.address.time = currentFormatDate;
      this.$toast.loading({ forbidClick: true });
      setTimeout(() => {
        this.$toast.clear();
        this.addAddress();
      }, 1200);
    },
    async addAddress() {
      const { data: res1 } = await this.$http.get("address", {
        params: { uid: this.$store.state.emailLogin[0].userID },
      });
      if (res1.status == 200) {
        if (res1.data.length >= 6) {
          this.$toast("地址最多为6个！");
        } else {
          if (this.address.isDefault === true) {
            this.editaddressFalse();
            setTimeout(() => {
              this.adddizhi();
            }, 500);
          } else {
            this.adddizhi2();
          }
        }
      }
    },
    async adddizhi() {
      const { data: res } = await this.$http.post("addAddress", this.address);
      if (res.status === 200) {
        this.$toast.success("添加成功");
        setTimeout(() => {
          this.setDefaultAddress(res.data[6]);
        }, 1000);
      }
    },
    async adddizhi2() {
      const { data: res } = await this.$http.post("addAddress", this.address);
      if (res.status === 200) {
        this.$toast.success("添加成功");
      }
    },
    //再改为默认地址
    async setDefaultAddress(item) {
      const { data: res } = await this.$http.post("setDefaultAddress", {
        time: item,
      });
      // console.log(res);
    },
    //修改所有地址为false
    async editaddressFalse() {
      const { data: res } = await this.$http.post("editAddressFalse", {
        id: this.$store.state.emailLogin[0].userID,
      });
      // console.log(res);
    },
  },
};
</script>
<style lang="less" scoped>
</style>